<?


// db used to do more, but I removed lots of crappy aspects of it. So now it's mostly a few static methods.

class db {

	function connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE) {
		if (!@mysql_connect($server, $username, $password)) {
			$e = new Error();
			$e->add(mysql_error());
			return $e;
		}
		
		if (!@mysql_select_db($database)) {
			$e = new Error();
			$e->add(mysql_error());
			return $e;
		}	
	}
	
	
	function sanitize_to_db($entry) {
		return mysql_real_escape_string($entry);
	}
	
	function sanitize_from_db($entry) {
		return trim(stripslashes($entry));
	}
	
	function get_local_datetime() {
		$args = func_get_args();
		$current_hour = TIMEZONE_OFFSET - substr(date('O'), 0, strlen(date('0')) - 3);
		$current_time = getdate();
		
		if ($args[0]) {
			// we accept the date/time designation passed
			return date($args[0],mktime($current_time['hours'] + $current_hour, $current_time['minutes'], $curent_time['seconds'], $current_time['mon'], $current_time['mday'], $current_time['year']));
		} else {
			// we return standard mysql timestamp
			return date('Y-m-d H:i:s',mktime($current_time['hours'] + $current_hour, $current_time['minutes'], $current_time['seconds'], $current_time['mon'], $current_time['mday'], $current_time['year']));
		}
	}
	
	function date_format($date) {
		return date(DATE_FORMAT, strtotime($date));
	}
	
	function time_format($time) {
		return date(TIME_FORMAT, strtotime($time));
	}
	
	function datetime_format($datetime) {
		return date(DATETIME_FORMAT, strtotime($datetime));
	}	
	
	function isError($obj) {
		if (is_object($obj)) {
			return $obj->isErrorObject;
		}
		
		return false;
	}
	
}

class Error {
	
	// simple error object
	
	var $_error = array();
	var $isErrorObject = true;
	
	function add($error) {
		if (is_object($error)) {
			// is an error object
			foreach($error->_error as $e) {
				$this->_error[] = $e;
			}
		} else {
			$this->_error[] = $error;
		}
	}
	
	function get() {
		return $this->_error[0];
	}
	
	function outputDialog($button_title, $button_url) {
		$args = func_get_args();
		$error = '<h1>error:</h1><div class="inset">';
		
		if ($args[2]) {
			$error .= $args[2] . '<br/><br/>';
		} else {
			if ($this) {
				foreach($this->_error as $e) {
					$error .= $e . '<br/>';
					$error .= '<br/>';
				}
			}
			
		}
		
		if ($button_title != null && $button_url != null) { 
			$error .= '<input type="button" class="button" value="' . $button_title . '" onclick="location.href=\'' . $button_url . '\'">';
	
			for ($i = 3; $i < count($args); $i = $i + 2) {
				$error .= '<input type="button" class="button" value="' . $args[$i] . '" onclick="location.href=\'' . $args[$i + 1] . '\'">';
			}
		}
		
		$error .= "</div>";
		print($error);
	}
	
	function outputList() {
		$error = "<div class=\"error-list\">";
		$error .= "<strong>The following error(s) occurred:</strong>\n";
		$error .= "<ul>";
		foreach($this->_error as $e) {
			$error .= '<li>' . $e . '</li>';
		}
		$error .= "</ul>";
		$error .= "</div>";
		print($error);
	}
	
	function outputErrorPage() {
		require_once(BASE_FULL_DIRECTORY.AUDITION_WEB_DIRECTORY."/layout/header.php");
		$this->outputList();
		require_once(BASE_FULL_DIRECTORY.AUDITION_WEB_DIRECTORY."/layout/footer.php");
		exit;
	}
	
	function hasErrors() {
		return (count($this->_error)  > 0);
	}
	
	function MySQL() { // static
		$e = new Error();
		$e->add(mysql_error());
		return $e;
	}
	
	function create($msg) { // static version of instantiating e, adding to it, and returning it. A shortcut.
		$e = new Error();
		$e->add($msg);
		return $e;
	}
	
	function mergeErrors($errorArray) {
		$e = new Error();
		foreach($errorArray as $_e) {
			if (db::isError($_e)) {
				$_error = $_e->_error;				
				foreach($_error as $es) {
					$e->add($es);
				}
			}
		}
		return $e;
	}
}

?>